package com.examp.shardingSphere.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.springframework.util.Assert;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

/**
 * @author liu
 */
public class BirthdayPreciseAlgorithm implements PreciseShardingAlgorithm<Date> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        String logicTableName = preciseShardingValue.getLogicTableName();
        Date value = preciseShardingValue.getValue();
        Assert.notNull(value, "birthday can not be null");
        Calendar instance = Calendar.getInstance();
        instance.setTime(value);
        return logicTableName + "_" + instance.get(Calendar.YEAR) + "_" + (instance.get(Calendar.MONTH) + 1);
    }
}
